/*
package cloud.xlh.filter;

import cloud.xlh.domin.vo.CommonRspVO;
import cloud.xlh.exceptionhandle.service.impl.BusinessExceptionEnum;
import cloud.xlh.infrastructure.cache.redis.utils.RedisUtil;
import cloud.xlh.infrastructure.utils.CommonUtil;
import cloud.xlh.utils.StackTraceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

*/
/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/1/13 15:08
 * @ Description: 防暴力请求url
 *//*

@Component
@Slf4j
public class IpUrlLimitFilter implements GlobalFilter, Ordered {
    @Autowired
    private RedisUtil redisUtil;

    private static final String LOCK_IP_URL_KEY="lock_ip_";

    private static final String IP_URL_REQ_TIME="ip_url_times_";

    private static final long LIMIT_TIMES=5;

    private static final int IP_LOCK_TIME=60;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        CommonRspVO commonRspVO = new CommonRspVO<>().builder
                .retCode(String.valueOf(BusinessExceptionEnum.IPLOCKED.getCode()))
                .retInfo(BusinessExceptionEnum.IPLOCKED.getMessage())
                .build();
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        if (ipIsLock(CommonUtil.getIpAddress(request))){
            //log.info("ip访问被禁止={}",IpAdrressUtil.getIpAdrress(httpServletRequest));
            //returnJson(httpServletResponse, JSON.toJSONString(commonRspVO));
            return response.setComplete();
        }
        if(!addRequestTime(CommonUtil.getIpAddress(request),request.getURI().getPath())){
            //returnJson(httpServletResponse, JSON.toJSONString(commonRspVO));
            return response.setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -600;
    }

    */
/**
     * @Description: 判断ip是否被禁用
     * @author: shuyu.wang
     * @date: 2019-10-12 13:08
     * @param ip
     * @return java.lang.Boolean
     *//*


    private Boolean ipIsLock(String ip){
        if(redisUtil.hasKey(LOCK_IP_URL_KEY+ip)){
            return true;
        }
        return false;
    }

    */
/**
     * @Description: 记录请求次数
     * @author: shuyu.wang
     * @date: 2019-10-12 17:18
     * @param ip
     * @param uri
     * @return java.lang.Boolean
     *//*


    private Boolean addRequestTime(String ip,String uri){
        log.info("开始记录请求次数，log print location is -> {}" , StackTraceUtil.getCurrentLocation(Thread.currentThread()));
        String key=IP_URL_REQ_TIME+ip+uri;
        if (redisUtil.hasKey(key)){
            log.info("自增，log print location is -> {}" , StackTraceUtil.getCurrentLocation(Thread.currentThread()));
            long time=redisUtil.incr(key,(long)1);
            System.out.println(time);
            if (time > LIMIT_TIMES){
                redisUtil.getLock(LOCK_IP_URL_KEY+ip,ip,IP_LOCK_TIME);
                log.error("ip->{}已被锁定，log print location is -> {}" , ip , StackTraceUtil.getCurrentLocation(Thread.currentThread()));
                return false;
            }
        }else {
            log.info("获取分布式锁，log print location is -> {}" , StackTraceUtil.getCurrentLocation(Thread.currentThread()));
            redisUtil.getLock(key,(long)1,1);
        }
        return true;
    }

    private void returnJson(HttpServletResponse response, String json) throws Exception {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/json; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(json);
        } catch (IOException e) {
            //log.error("LoginInterceptor response error ---> {}", e.getMessage(), e);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}
*/
